package com.enterprise.controller.manage;


import com.enterprise.controller.BaseController;
import com.enterprise.entity.Resources;
import com.enterprise.entity.Role;
import com.enterprise.entity.page.PageModel;
import com.enterprise.service.RoleService;
import com.enterprise.service.RoleresService;
import com.enterprise.service.Services;
import com.enterprise.service.ResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/manage/resources/")
public class ResourcesAction extends BaseController<Resources>{
	private static final String page_toList = "/manage/resources/resourcesList";
	private static final String page_toEdit = "/manage/resources/resourcesInfo";
	private static final String page_toAdd = "/manage/resources/resourcesInfo";
	private static final String page_toRoleList = "/manage/resources/roleResourcesList";
	public ResourcesAction() {
		super.page_toList = page_toList;
		super.page_toEdit = page_toEdit;
		super.page_toAdd = page_toAdd;
	}

	@Autowired
	private ResourcesService resourcesService;

	@Autowired
	private RoleService roleService;

	@Autowired
	private RoleresService roleresService;

	@Override
	public Services<Resources> getService() {
		return resourcesService;
	}

	// 添加权限
	@RequestMapping("setResources")
	public String setResources(HttpServletRequest request, String[] roleIds, String resourcesId, @ModelAttribute("e") Resources e, RedirectAttributes flushAttrs) throws Exception {
		roleresService.setResourcesRole(roleIds,resourcesId);
		addMessage(flushAttrs,"操作成功！");
		return "redirect:selectList";
	}

	// 删除
	@RequestMapping("delResources")
	public String delResources(HttpServletRequest request, String[] roleIds, String resourcesId, @ModelAttribute("e") Resources e, RedirectAttributes flushAttrs) throws Exception {
		roleresService.delResourcesRole(roleIds,resourcesId);
		addMessage(flushAttrs,"操作成功！");
		return "redirect:selectList";
	}

	// 查询按钮 并跳转页面
	@RequestMapping("toRoleRess")
	public String toRoleRess(HttpServletRequest request,String[] roleIds,String[] resourcesIds,@ModelAttribute("e") Resources e,RedirectAttributes flushAttrs) throws Exception {
		resourcesService.toRoleRes(roleIds,resourcesIds);
		addMessage(flushAttrs,"操作成功！");
		return "redirect:selectList";
	}
	// 跳转角色分配权限页面
	@RequestMapping("toRoleRes")
	public String toRoleRes(HttpServletRequest request,@ModelAttribute("e") Role e){
		String resourcesid = request.getParameter("resourcesid");
		this.initPageSelect();
		int offset = 0;
		if(request.getParameter("pager.offset")!=null){
			offset = Integer.parseInt(request.getParameter("pager.offset"));
		}
		if(offset < 0){
			offset=0;
		}
		e.setOffset(offset);
		e.setRoleid(Long.valueOf(resourcesid));
		PageModel page = roleService.selectPowerPageList(e);
		if(page == null){
			page = new PageModel();
		}
		page.setPagerSize((page.getTotal() + page.getPageSize() - 1)
				/ page.getPageSize());
		selectListAfter(page);
		page.setDataid(Integer.parseInt(resourcesid));
		request.setAttribute("pager", page);
		return page_toRoleList;
	}

}
